package com.wxtl.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.wxtl.dao.ContentDAO;
import com.wxtl.entity.Content;
import com.wxtl.util.DataGridModel;
import com.wxtl.util.Page;

@Service
public class ContentService {
	private ContentDAO contentDAO;
	
	public ContentDAO getContentDAO() {
		return contentDAO;
	}

	public void setContentDAO(ContentDAO contentDAO) {
		this.contentDAO = contentDAO;
	}

	public void delete(int[] ids){
		contentDAO.delete(ids);
	}
	
	public void add(Content content){
		content.setWholeContent(getWholeContent(content));
		contentDAO.add(content);
	}
	
	public void update(Content content){
		content.setWholeContent(getWholeContent(content));
		contentDAO.update(content);
	}
	
	public Page query(DataGridModel dgm,Content content){
		return contentDAO.query(dgm,content);
	}
	
	public List<Content> queryAllContent(){
		return contentDAO.queryAllContent();
	}
	
	public Content getContentById(int id){
		return contentDAO.load(id);
	}
	
	public String getWholeContent(Content c){
		StringBuffer sb = new StringBuffer();
		List<String> list = new ArrayList<String>();
		list.add(c.getContent());
		
		while(c.getParentId()!=null){
			c = getContentById(c.getParentId());
			list.add(c.getContent());
		}
		
		for(int i=list.size()-1;i>-1;i--){
			if(i==0){
				sb.append(list.get(i));
				continue;
			}
			sb.append(list.get(i)+"/");
		}
		return sb.toString();
	}
}
